﻿using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public class PB_StorageBusiness : BaseBusiness<PB_Storage>, IPB_StorageBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public PB_StorageBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<PB_StorageDTO>> GetDataListAsync(PB_StoragePageInput input)
        {
            var search = input.Search;
            Expression<Func<PB_Storage, Base_Department, PB_StorageDTO>> select = (a, b) => new PB_StorageDTO
            {
                DepartmentName = b.Name,
                deptid=b.Id,
                
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.DepartmentId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    select @select.Invoke(a, b);

            if (!search.Keyword.IsNullOrEmpty())
            {
                var keyword = $"%{search.Keyword}%";
                q = q.Where(x =>
                      EF.Functions.Like(x.Name, keyword)
                      || EF.Functions.Like(x.Code, keyword));
            }
            q = q.Where(m => _operator.Property.DeptIds.Contains(m.DepartmentName));
           q= q.Where(x => _operator.Property.DeptIds.Contains(x.deptid));
            return await q.GetPageResultAsync(input);
        }

        public async Task<PB_Storage> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        [DataAddLog(UserLogType.仓库管理, "Name", "仓库")]
        [DataRepeatValidate(new string[] { "Code", "Name" }, new string[] { "编号", "名称" })]
        public async Task AddDataAsync(PB_Storage data)
        {
            //var abcObject = new
            //{
            //    //标识
            //    Key = "QRCODE_ZY_0005",
            //    //json
            //    Value = "@{\"id\":\"" + data.Id + "\",\"DepartmentId\":\"" + data.DepartmentId + "\"}"
            //};
            //data.QrCode = Extention.Base64Encode(JsonConvert.SerializeObject(abcObject));
            await InsertAsync(data);
        }

        [DataEditLog(UserLogType.仓库管理, "Name", "仓库")]
        [DataRepeatValidate(new string[] { "Code", "Name" }, new string[] { "编号", "名称" })]
        public async Task UpdateDataAsync(PB_Storage data)
        {
            //var abcObject = new
            //{
            //    //标识
            //    Key = "QRCODE_ZY_0005",
            //    //json
            //    Value = "@{\"id\":\"" + data.Id + "\",\"DepartmentId\":\"" + data.DepartmentId + "\"}"
            //};
            //data.QrCode = Extention.Base64Encode(JsonConvert.SerializeObject(abcObject));
            await UpdateAsync(data);
        }

        [DataDeleteLog(UserLogType.仓库管理, "Name", "仓库")]
        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task ModifyDefaultAsync(PB_Storage data)
        {
            var oldDefaultData = await GetIQueryable().Where(p => p.IsDefault == true).FirstOrDefaultAsync();
            oldDefaultData.IsDefault = false;

            var modifyData= new List<PB_Storage>() { data, oldDefaultData };

            await UpdateAsync(modifyData);
        }

        public async Task<List<PB_Storage>> QueryStorageDataAsync()
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_Storage>();

            return await q.Where(where).ToListAsync();
        }

        public async Task ModifyIsTrayAsync(string Id)
        {
            var entity = await GetEntityAsync(Id);
            if (entity.IsTray)
            {
                entity.IsTray = false;
                if (entity.IsZone) entity.IsZone = false;
            }
            else
            {
                entity.IsTray = true;
            }

            await UpdateAsync(entity);
        }

        public async Task ModifyIsZoneAsync(string Id)
        {
            var entity = await GetEntityAsync(Id);
            if (entity.IsZone)
            {
                entity.IsZone = false;
            }
            else
            {
                entity.IsZone = true;
            }

            await UpdateAsync(entity);
        }

        public async Task ModifyDisableAsync(string Id)
        {
            var entity = await GetEntityAsync(Id);
            if (entity.Disable)
            {
                entity.Disable = false;
            }
            else
            {
                entity.Disable = true;
            }

            await UpdateAsync(entity);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}